package com.cn.unarmed.adapter;

import android.app.Activity;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.cn.unarmed.activity.R;
import com.cn.unarmed.zlistview.adapter.BaseSwipeAdapter;
import com.cn.unarmed.zlistview.enums.DragEdge;
import com.cn.unarmed.zlistview.enums.ShowMode;
import com.cn.unarmed.zlistview.listener.OnSwipeLayoutListener;
import com.cn.unarmed.zlistview.listener.SimpleSwipeListener;
import com.cn.unarmed.zlistview.widget.ZSwipeItem;

import java.util.List;

/**
 * Created by Administrator on 2017/6/5.
 */
public class SwipeItemAdapter extends BaseSwipeAdapter {

    private List<String> list;
    private Activity context;
    private ZwipeAdapterOnClickListener listener;

    public SwipeItemAdapter(Activity context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_list_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return context.getLayoutInflater().inflate(R.layout.item_swipe_list,
                parent, false);
    }

    @Override
    public void fillValues(final int position, View convertView) {
        final ZSwipeItem swipeItem = (ZSwipeItem) convertView
                .findViewById(R.id.swipe_list_item);
        swipeItem.setShowMode(ShowMode.PullOut);
        swipeItem.setDragEdge(DragEdge.Right);

        final Holder holder = new Holder();
        holder.toggleButton = (ToggleButton)swipeItem.findViewById(R.id.device_status);
        holder.textView_delete = (TextView)swipeItem.findViewById(R.id.txt_delete);
        holder.textView = (TextView)swipeItem.findViewById(R.id.text_swipe);


        holder.textView_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swipeItem.close();
                listener.OnDelete(list.get(position));
            }
        });

        holder.toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swipeItem.close();
                listener.OnStatusChage(holder.toggleButton.isChecked(),list.get(position));
            }
        });
        holder.textView.setText(list.get(position));


        swipeItem.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(ZSwipeItem layout) {
            }

            @Override
            public void onClose(ZSwipeItem layout) {
            }

            @Override
            public void onStartOpen(ZSwipeItem layout) {
                listener.OnClick(list.get(position));
            }

            @Override
            public void onStartClose(ZSwipeItem layout) {
                listener.OnClick(list.get(position));
            }

            @Override
            public void onHandRelease(ZSwipeItem layout, float xvel, float yvel) {
            }

            @Override
            public void onUpdate(ZSwipeItem layout, int leftOffset,
                                 int topOffset) {
            }
        });
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(ZwipeAdapterOnClickListener listener) {
        this.listener = listener;
    }

    public interface ZwipeAdapterOnClickListener {
        void OnClick(String mode);

        void OnDelete(String mode);

        void OnStatusChage(boolean b, String mode);
    }

    private class Holder {
        ToggleButton toggleButton;
        TextView textView, textView_delete;
    }
}
